//
//  HomecellDetailViewController.m
//  EasyToTesco
//
//  Created by mc on 16/3/3.
//  Copyright © 2016年 Jack. All rights reserved.
//

#import "HomecellDetailViewController.h"
#import "HeaderFileForEasyToTesco.h"
#import "AFNetworking.h"
#import "UIImageView+WebCache.h"

//引入cell的头文件
#import "ShoperDetailOneTableViewCell.h"
#import "ShoperDetailTwoTableViewCell.h"

@interface HomecellDetailViewController ()<UITableViewDataSource, UITableViewDelegate>
{
    //判断是否显示全部评价
    BOOL judgeCell;
    
    
    
}
@property (nonatomic, strong)NSMutableArray *CellDetailArray;

@property (nonatomic, strong)UITableView *homeCellDetailtableview;
@end

@implementation HomecellDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    
    
    
    [self ShoperDetailInitSomething];
    
    [self homecelldetailUI];
    
    [self homeCellDetailtableviewUI];
    
    [self addgoodDetaildatasources];
    
    
    
}

- (void)ShoperDetailInitSomething {
    judgeCell = YES;
    self.CellDetailArray = [@[]mutableCopy];
}

- (void)homeCellDetailtableviewUI{
    self.homeCellDetailtableview = [[UITableView alloc] initWithFrame:CGRectMake(0, HIGHT*0.1, WEITH, HIGHT*0.8+20) style:UITableViewStylePlain];
    self.homeCellDetailtableview.dataSource = self;
    self.homeCellDetailtableview.delegate = self;
    [self.view addSubview:self.homeCellDetailtableview];
}

- (void)homecelldetailUI {
    [self addhomecelldetailtopView];
    
    
    [self addlastView];
}


- (void) addlastView {
    UIView *lastView = [[UIView alloc] initWithFrame:CGRectMake(0, HIGHT*0.9+20, WEITH, HIGHT*0.1-20)];
    [self.view addSubview:lastView];
    lastView.userInteractionEnabled = YES;
    NSArray *array= @[@"加入购物车", @"立即购买"];
    
    for (NSInteger i=0; i<2; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake((WEITH/2)*i, 0, WEITH/2, HIGHT*0.1-20);
        button.titleLabel.font = [UIFont systemFontOfSize:14];
        button.titleLabel.textAlignment = NSTextAlignmentCenter;
        [button setTitle:array[i] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(lastBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = 330 + i;
        if (i == 1) {
            button.backgroundColor = [UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1];
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        }else {
            button.backgroundColor = [UIColor colorWithRed:244/255. green:244/255. blue:244/255. alpha:1];
            [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        }
        
        
        [lastView addSubview:button];
    }
}

- (void)lastBtnClick:(UIButton *)button {
    if (button.tag == 330) {
<<<<<<< HEAD
        NSLog(@"....");
=======
        
>>>>>>> dd1aac4040986135177ad18eb7781566439f59ee
    } else{
        NSLog(@"加入购物车");
//        OrderDetailViewController *orderDetailVC = [[OrderDetailViewController alloc] init];
//        [self presentViewController:orderDetailVC animated:YES completion:nil];
    }
}

- (void)addhomecelldetailtopView {
    UIImageView *image1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, (self.secondView.frame.size.height-20)/2, 20, 20)];
    image1.image = [UIImage imageNamed:@"goback-icon.png"];
    [self.secondView addSubview:image1];
    self.leftBtn.hidden = YES;
    
    UIButton *goBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    goBackBtn.frame = CGRectMake(0, 0, self.secondView.frame.size.height, self.secondView.frame.size.height);
    [goBackBtn addTarget:self action:@selector(gobackClick) forControlEvents:UIControlEventTouchUpInside];
    [self.secondView addSubview:goBackBtn];
    self.titleLabel.text = @"商品详情";
    
    
    
}

//返回上一级页面
- (void)gobackClick {
    [self dismissViewControllerAnimated:YES completion:nil];
}


#pragma mark -- UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0) {
        return 1;
    }else if (section == 1){
        return 1;
    }else {
        if (self.CellDetailArray.count!=0) {
            if (judgeCell == YES) {
                return 2;
            } else {
                return self.CellDetailArray.count;
            }
        } else {
            return 0;
        }
        
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        static NSString *identifier = @"ShoperDetailOneTableViewCell";
        ShoperDetailOneTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"ShoperDetailOneTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
//        NSLog(@"--------%@,,,%@,,,,%@,,,,%@,,,,%@", self.introStr,self.Pricenew,self.goodsiID,self.imagestr,self.oldprice);
//        
//        [cell.picImage sd_setImageWithURL:[NSURL URLWithString:self.imagestr] placeholderImage:[UIImage imageNamed:@"xiangqing.png"]];
//        cell.Titlelabel.text = [NSString stringWithFormat:@"减价%@", self.introStr];
////        cell.starView.
//        cell.oldPriceLabel.text = [NSString stringWithFormat:@"原价%@", self.oldprice];
//        cell.PriceNewLabel.text = [NSString stringWithFormat:@"现价%@", self.Pricenew];
        
        
        return cell;
        
    } else if (indexPath.section == 1) {
        static NSString *identifier = @"shoperDetailTwocell";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.font = [UIFont systemFontOfSize:14];
        cell.textLabel.textColor = [UIColor grayColor];
        cell.textLabel.text = @"暂无介绍";
        return cell;
    } else {
        static NSString *identifier = @"ShoperDetailTwoTableViewCell";
        ShoperDetailTwoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (cell == nil) {
            cell = [[[NSBundle mainBundle]loadNibNamed:@"ShoperDetailTwoTableViewCell" owner:nil options:nil] lastObject];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
            cell.shoperTwoPic.layer.cornerRadius = CGRectGetHeight(cell.shoperTwoPic.frame)/2;
            cell.shoperTwoPic.layer.masksToBounds = YES;
        }
        return cell;
    }
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
#pragma mark -- UITableViewDelegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        return HIGHT * 0.4;
    } else if (indexPath.section == 1) {
        return 30;
    } else {
        return 70;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return 0;
    } else {
        return 30;
    }
}
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    if (section == 2) {
        return 0;
    } else {
        return 10;
    }
}
- (nullable UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    if (section == 0) {
        return nil;
    } else {
        UIView *headerView = [[UIView alloc] init];
        headerView.backgroundColor = [UIColor whiteColor];
        
        headerView.userInteractionEnabled = YES;
        UILabel *titlabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 5, 200, 20)];
        titlabel.textAlignment = NSTextAlignmentLeft;
        titlabel.font = [UIFont systemFontOfSize:16];
        titlabel.textColor = [UIColor blackColor];
        [headerView addSubview:titlabel];
        if (section == 1) {
            titlabel.text = @"商品简介";
        } else {
            titlabel.text = [NSString stringWithFormat:@"商品评价(%ld)", self.CellDetailArray.count];
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(CGRectGetMaxX(titlabel.frame)+10, 5, WEITH-CGRectGetMaxX(titlabel.frame)-10, 20);
            [button setTitle:@"加载更多评价" forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont systemFontOfSize:14];
            button.titleLabel.textAlignment = NSTextAlignmentRight;
            [button setTitleColor:[UIColor colorWithRed:52/255. green:147/255. blue:225/255. alpha:1] forState:UIControlStateNormal];
            [button addTarget:self action:@selector(addjudgeBtnClick:) forControlEvents:UIControlEventTouchUpInside];
            [headerView addSubview:button];
            
        }
        
        return headerView;
    }
}

//判断是否加载更多的评价
- (void)addjudgeBtnClick:(UIButton *)button {
    if (judgeCell == YES) {
        [button setTitle:@"收回更多评价" forState:UIControlStateNormal];
    } else {
        [button setTitle:@"加载更多评价" forState:UIControlStateNormal];
    }
    
    judgeCell = !judgeCell;
    [self.homeCellDetailtableview reloadData];
}

- (nullable UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *footView = [[UIView alloc] init];
    footView.backgroundColor = [UIColor grayColor];
    return footView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    
}

//获取产品评价
- (void)addgoodsvaluedatasources{
        AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *goods_id = self.goodsiID;
    

    NSDictionary *param = @{@"goods_id":[NSString stringWithFormat:@"%@", goods_id]};
    
    [mgr POST:@"http://www.ylgw8.com/AppGoods/Getcppj" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        
        NSLog(@"%@", string);
        
        [self.homeCellDetailtableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
//         NSLog(@"数据请求错误原因:%@", error);
//         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}

//获取产品详情
- (void)addgoodDetaildatasources{
    AFHTTPRequestOperationManager *mgr = [AFHTTPRequestOperationManager manager];
    mgr.responseSerializer = [AFHTTPResponseSerializer serializer];
    
    NSString *goods_id = self.goodsiID;
    NSString *username = @"18620614920";
    
    NSDictionary *param = @{@"goods_id":[NSString stringWithFormat:@"%@", goods_id], @"username":[NSString stringWithFormat:@"%@", username]};
    
    [mgr POST:@"http://www.ylgw8.com/AppShop/Getcpinfo" parameters:param success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSString *string = [[NSString alloc] initWithData:responseObject encoding:NSUTF8StringEncoding];
        
        [self addgoodsvaluedatasources];
        NSLog(@"的啊\%@", string);
        
        [self.homeCellDetailtableview reloadData];
    } failure:^(AFHTTPRequestOperation *operation,NSError *error)
     {
         //         operation.response.statusCode;//404 401之类
         //         NSLog(@"数据请求错误原因:%@", error);
         //         NSLog(@"数据请求错误码:%ld", (long)operation.response.statusCode);
     }];
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
